#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 500050;

struct Node {LL x, pos;} p[1000010];

int n, K[N], cnt, tot;
LL ans = 1e18, num, *a[N], mx = 1, b[N], res, Ans[1000010], c;
unordered_map<int, int> M;

bool cmp(Node x, Node y) {
    return x.x < y.x;
}

int read() {
	int rd = 0, z = 1; char ch = getchar(); 
	while (ch < '0' || ch > '9') {if (ch == '-') z = -1; ch = getchar();}
	while (ch <= '9' && ch >= '0') rd = rd * 10 + ch - '0', ch = getchar();
	return z * rd; 
}

int main() {
    freopen("list.in", "r", stdin);
    freopen("list.out", "w", stdout);
    // int st = clock();
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &K[i]); 
        if (K[i] == 0) {
            // cout << "qwq" << endl;
            n--, i--; continue;
        }
        a[i] = new LL[K[i] + 5]();
        for (int j = 1; j <= K[i]; j++)
            a[i][j] = read();
        for (int j = 2; j <= K[i]; j++) {
            p[++cnt] = {a[i][j] - a[i][j - 1] + 1, a[i][j]};
            mx = max(mx, a[i][j] - a[i][j - 1] + 1);
        }
    }
    scanf("%lld", &c);
    for (int i = 1; i <= n; i++) {
        scanf("%lld", &b[i]); b[i] = i * b[i];
    }
    // cout << "qwq" << endl;
    for (int i = 1; i <= n; i++) {
        for (int j = 2; j <= K[i]; j++) {
            res += (a[i][j] - a[i][j - 1]) * c;
        }
        int x = M[a[i][1]]; M[a[i][1]]++;
        res -= b[x]; res += b[x + 1];
    }
    res += n * mx * c;
    // cout << "res = " << res << " cnt = " << cnt << endl;
    p[++cnt] = {1, 0};
    sort(p + 1, p + cnt + 1, cmp);
    p[cnt + 1].x = 2e9; num = n;
    int cur = 0;
    // cout << "res = " << res << endl;
    for (int i = cnt; i >= 1; i--) {
        if (p[i].x != p[i + 1].x) {
            // cout << "x = " << p[i].x << " res = " << res << endl;
            if (res < ans) {
                ans = res; tot = 0;
            }
            if (res == ans) {
                Ans[++tot] = p[i].x;
            }
        }
        int x = M[p[i].pos]; M[p[i].pos]++;
        res -= b[x]; res += b[x + 1];
        // cout << "res = " << res << endl;
        cur++;
        if (p[i].x != p[i - 1].x) {
            res -= num * c; 
            num += cur; cur = 0; 
            res -= num * c * (p[i].x - p[i - 1].x - 1);
        }
    }
    tot = unique(Ans + 1, Ans + tot + 1) - Ans -  1;
    printf("%lld %d\n", ans, tot);
    for (int i = 1; i <= tot; i++)
        printf("%lld ", Ans[i]);
    // cout << "time = " << clock() - st << endl;
    return 0;
}